package org.msfj.toilets.data.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import org.msfj.toilets.config.ToiletConfig;
import org.msfj.toilets.vo.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;

/**
 * @Description:
 * @Author: like
 * @Date: 2018-08-27 16:46
 * @Version: 1.0
 * @Created in idea by autoCode
 */
@Controller
public class WebsocketController {

    @Autowired
    ToiletConfig toiletConfig;

    @MessageMapping("/init")
    @SendTo("/data")
    public String init() {
        DataResult dataResult = new DataResult();
        dataResult.setType(1);
        String men = toiletConfig.getMen();
        String women = toiletConfig.getWomen();
        String other = toiletConfig.getOther();
        if (StrUtil.isNotBlank(men)) {
            dataResult.setMenCount(men.split(",").length);
        }
        if (StrUtil.isNotBlank(women)) {
            dataResult.setWomenCount(women.split(",").length);
        }
        if (StrUtil.isNotBlank(other)) {
            dataResult.setOtherCount(other.split(",").length);
        }
        return JSONUtil.toJsonStr(dataResult);
    }
}
